#include "ParkinglotDB.h"
#define _CRT_SECURE_NO_DEPRECATE 1

const CvPoint EntryPoint(cvPoint(100,100));

std::map<int, Car>::iterator ParkinglotDB::FindCar(int license_plate)
{
	if(!license_plate)
		return NULL;
	it = Car_list.begin();  
	while (it != Car_list.end())
		if (it->second.license_plate == license_plate ) 
			return it;
		else it++;
		return NULL; 
}


void ParkinglotDB::InitLot()
{
	char line[256];
	int i=0;
	ifstream myfile ("parking.txt");
	if (myfile.is_open())
	{
		while (!myfile.eof())
		{
			myfile.getline(line,256);
			//getline (myfile,line);
			ParkingLot_list.insert(pair<int,ParkingLot>(i,ParkingLot()));
			//This STRTOK thing has extremely strange syntex
			ParkingLot_list[i].x1=atoi(strtok(line," ,"));
			ParkingLot_list[i].y1=atoi(strtok(NULL," ,"));
			ParkingLot_list[i].x2=atoi(strtok(NULL," ,"));
			ParkingLot_list[i].y2=atoi(strtok(NULL," \n"));
			ParkingLot_list[i].carLP=0;
			i++;
		}
		myfile.close();
	}
	else cout << "Unable to open file"; 
	return;
}

/*this will generate a new car in the parking lot*/
void ParkinglotDB::CarEntered(int car_id)
{
	Car_list.insert(pair<int,Car>(car_id,Car())); 
	Car_list[car_id].car_id = car_id; 
	time_t rawtime;
	time (&rawtime );
	Car_list[car_id].entry_time=ctime (&rawtime);
	carArrived(EntryPoint, car_id );
}

/*this will delete a car from the parking lot*/
void ParkinglotDB::CarExits(int car_id)
{
	Car_list.erase(car_id); 
}

/*sets the license plate of an existing car, returns false if car_id is not in the database*/
bool ParkinglotDB::SetLicensePlate (int car_id, int license_plate)
{
	Car_list[car_id].license_plate = license_plate; 
	return true;
}

/*sets the parking place of an existing car, returns false if car_id is not in the database */
bool ParkinglotDB::CarParked (int car_id, int x, int y)
{
	if(!Car_list[car_id].car_id)
		return false; //no such car, wtf you want ? 
	Lotit = ParkingLot_list.begin();  
	while (Lotit != ParkingLot_list.end())
	{
		ParkingLot &p=Lotit->second;
		if(p.x1 < x && p.y1<y && p.x2>x && p.y2>y)
		{
			if(p.carID>=0)
				return false;//Parking is taken, vehicle manslaughter
			p.carLP=Car_list[car_id].license_plate;
			p.carID=car_id;
			Car_list[car_id].car_parking=Lotit->first+1;
			return true;
		}
		Lotit++;
	}
	return false; //no such parking lot.
}

/*returns the car_id of the car in the parking place of (x,y), 
and updating the database accordingly*/
int ParkinglotDB::CarUnparked(int x, int y)
{
	Lotit = ParkingLot_list.begin();  
	while (Lotit != ParkingLot_list.end())
	{
		ParkingLot &p=Lotit->second;
		if(p.x1 < x && p.y1<y && p.x2>x && p.y2>y)
		{
			int rval=p.carLP;
			//it=FindCar(p.carLP);
			if(p.carID ==-1)
				return -1; //parking is empty WTF!!!
			Car_list[Lotit->second.carID].car_parking=-1;
			Lotit->second.carLP=0;
			Lotit->second.carID=-1;
			return rval;
		}
		Lotit++;
	}
	return 0;//No such parking exists
}

/*returns some description of the car location*/ 
int ParkinglotDB::WhereIsMyCar(int license_plate)
{
	it = FindCar(license_plate); 
	if (it == NULL)
		return -2;
	else
		return it->second.car_parking;
}

/*prints the occupied parking places*/ 
void ParkinglotDB::PrintOccupiedSpots()
{
	//	ofstream file;
	//	file.open ("LogOcc.txt");
	//	streambuf* sbuf = cout.rdbuf();
	//	cout.rdbuf(file.rdbuf());
	cout << "             LOG \n";
	Lotit = ParkingLot_list.begin();  
	int i=1;
	while (Lotit != ParkingLot_list.end())
	{
		if(Lotit->second.carLP)
		{
			cout <<"Car: " << Lotit->second.carLP << ", Is parking in : " << i;
			cout << "\n";
		}
		Lotit++;
		i++;
	}
	//	cout.rdbuf(sbuf);
	return;
}

/*returns the time , when an existing car has entered*/ 
string ParkinglotDB::WhenEntered(int license_plate)
{
	it = FindCar(license_plate);
	if (it == NULL)
		return "Car Is Not Here"; 
	else
		return it->second.entry_time; 

}

/* prints log to TXT file the describes for every parking is it empty or who parking there, 
and when he entered to the parking lot*/
void ParkinglotDB::PrintLogToFile()
{

	ofstream file;
	file.open ("Log.txt");
	streambuf* sbuf = cout.rdbuf();
	cout.rdbuf(file.rdbuf());
	cout << "					     LOG \n";
	it = Car_list.begin();  
	while (it != Car_list.end())
	{
		cout <<"Car: " << it->second.license_plate;
		cout <<" , is in parking number :" << it->second.car_parking;
		cout <<", Entered in: " << it++->second.entry_time;
		cout << "\n";
	}
	cout.rdbuf(sbuf);
	file.close();
	return;
}

int ParkinglotDB::IsParkingAvailble(int parking_id)
{
	if(ParkingLot_list[parking_id].carID>=0)
		return ParkingLot_list[parking_id].carLP;
	return -1;
}

bool ParkinglotDB::isThisParkingSlot(int x, int y)
{
	Lotit = ParkingLot_list.begin();  
	while (Lotit != ParkingLot_list.end())
	{
		ParkingLot &p=Lotit->second;
		if(p.x1 < x && p.y1<y && p.x2>x && p.y2>y)
			return true;
		Lotit++;
	}
	return false; //no such parking lot.
}